#ifndef _MODEL_FILE_H_
#define	_MODEL_FILE_H_

#include "platform.h"



#define __PACK__  __attribute__((packed))

typedef struct t_TimerData {
    int8_t    mode;             ///< timer trigger source -> virtual button;
    uint16_t  start:12;         ///< timer start from value
    uint16_t  countdownBeep:1;  ///< beep when last 30 seconds
    uint16_t  minuteBeep:1;     ///< beep enable for every minite
    uint16_t  persistent:1;     ///< timer value be saved when power down
    uint16_t  spare:1;          ///<
    uint16_t  value;            ///< seconds left
} __PACK__ TimerData;

typedef struct t_VFunction {
    uint16_t source;
    uint16_t functionID;
    void *   functionData;
}__PACK__ VFunction;

/// trainer port setting for 4 trainer channel
typedef struct t_TrainerData {
    int16_t calib;              ///< calibration offsets for 4 sticks
    uint8_t srcChn:6;           ///< source channel 0-7 = ch1-8
    uint8_t mode:2;             ///< 0:off, 1:add-mode; 2: substignore teacher-mode
}__PACK__ TrainerData;

typedef struct t_ServoData {
    int8_t  min;                ///< max
    int8_t  max;                ///< min
    int8_t  ppmCenter;          ///< center
    int16_t offset:14;          ///<
    uint16_t symetrical:1;      ///<
    uint16_t revert:1;          ///< revert

}__PACK__ ServoData;

typedef struct t_HeliData { // Swash Ring data
    uint8_t   invertELE:1;
    uint8_t   invertAIL:1;
    uint8_t   invertCOL:1;
    uint8_t   type:5;  
    uint8_t   collectiveSource;
    uint8_t   value;
}__PACK__ HeliData;

typedef struct t_PhaseData {
    char name[LEN_FP_NAME];     ///< flight phase name
}__PACK__ PhaseData;

typedef  struct{
    int8_t   type;              ///< 0, curve; 1, broken lines
    int16_t  points[7];
}__PACK__ CurveData;

typedef struct t_MixToData {
    uint8_t destCh:4;           ///< mix to
    uint8_t curveMode:1;        ///< O, curve; 1, differential
    uint8_t noExpo:1;
    uint8_t weightMode:1;
    uint8_t offsetMode:1;
    int16_t points[7];
    int8_t  weight;
    int8_t  swtch:6;
    uint8_t mltpx:2;            ///< multiplex method: 0 means +=, 1 means *=, 2 means :=
    uint8_t phases:5;
    int8_t  carryTrim:3;
    uint8_t srcRaw:6;           ///< source channel
    uint8_t mixWarn:2;          ///< mixer warning
    uint8_t delayUp:4;
    uint8_t delayDown:4;
    uint8_t speedUp:4;
    uint8_t speedDown:4;
    int8_t  curveParam;
    int8_t  offset;
}__PACK__ MixData;


///< model file
typedef struct t_ModelFile {
    char checkSum;
    char      name[LEN_MODEL_NAME]; ///< Name
    uint8_t   modelId;          ///< ID
    uint8_t   modelBmpId;
    TimerData timers[2];        ///< timers
    TrainerData trainerCh[4];	  ///< trainer port
    uint8_t   studWeight;       ///< student weight; 
    uint8_t   trainerOff;       ///< source for set trainer;
    uint8_t   protocol:3;       ///< communication protocols
    int8_t    ppmNCH:4;         ///< number of channels
    uint8_t   trimInc:3;        ///< Trim Increments
    int8_t    trimValue;        ///< trim value
    uint8_t   disableThrottleWarning:1;    ///< 
    uint8_t   pulsePol:1;       ///< pulse polization
    int8_t    ppmDelay;             ///<
    ///      BeepANACenter beepANACenter;    // 1<<0->A1.. 1<<6->A7
    MixData   mixData[32];          ///< mixer
    ServoData servoData[16];        ///< limit
    CurveData  expoData[16];         ///< expo 
  
    /// CURVTYPE  curves[16];           ///< curves
    int16_t   failsafe[16];         ///< failsafe positions, 0, no pulse; 1-2046; 2047, hold last position
  
    HeliData  heli;                 ///< heli ccpm and swashring
    PhaseData phaseData[6];         ///< flight phase

    int8_t    ppmFrameLength;       ///< 0=22.5ms  (10ms-30ms) 0.5ms increments
    uint8_t   thrTraceSrc;
  
    ///swstate_t switchWarningStates;

    /// functions
    VFunction vFunctions[4];    ///< 4 functions 
}__PACK__ ModelFile;




#endif
